package com.zch.rpc.client.client;

import com.zch.rpc.register.register.ServiceDiscovery;

import java.lang.reflect.Proxy;

/**
 * 需要借助zoopeeker协调中心
 */
public class RpcClientProxy2 {
    private ServiceDiscovery serviceDiscovery;

    public RpcClientProxy2(ServiceDiscovery serviceDiscovery) {
        this.serviceDiscovery = serviceDiscovery;
    }

    /**
     * 生成代理对象
     * @param interfaceClazz 代理类型（接口）
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T>T getProxy(Class<T> interfaceClazz){
        ProxyHandler<T> invocationHandler=new ProxyHandler<T>(serviceDiscovery,interfaceClazz,"");
        // clazz不是接口不能使用JDK动态代理
        return (T) Proxy.newProxyInstance(interfaceClazz.getClassLoader(), new Class<?>[]{interfaceClazz}, invocationHandler);
    }
}
